<?php

declare(strict_types=1);

namespace app\common\service\sys;

use app\common\traits\ServiceTrait;
use app\common\dao\sys\UserGroupMemberDao;

/**
 * 系统用户组成员关联表 服务业务层(Service)
 * @author kaige
 * @update  2022-07-13
 * @copyright ©2022 泉州市盛筑信息科技有限公司 版权所有
 */
class UserGroupMemberService
{
  protected $dao = null;

  public function __construct()
  {
    $this->dao = new UserGroupMemberDao();
  }
  use ServiceTrait;

  /**
   * 重写 关联 with
   */
  public function _list_with()
  {
    return ['gluser'];
  }

  /**
   * 重写 只显示关联的字段
   */
  public function _list_visible()
  {
    return ['gluser' => ['id', 'un', 'real_name', 'dept_id_name', 'ranking_id_name', 'avatar']];
  }

  /**
   * 重写 列表过滤条件
   * @param $post
   * @return array
   */
  public function _list_where($post)
  {
    $where = [];
    if (isset($post['group_id']) && $post['group_id'] != "") {
      $where[] = ['group_id', '=', $post['group_id']];
    }
    if (isset($post['uid']) && $post['uid'] != "") {
      $where[] = ['uid', '=', $post['uid']];
    }

    $result['where'] = $where;
    $result['field'] = [];
    $result['order'] = ['id desc'];
    return $result;
  }

  /**
   * 重写 数据格式化
   */
  public function _list_format($list)
  {
    foreach ($list as $k => &$v) {
      if (isset($v['gluser']['avatar'])) {
        $list[$k]['avatar'] = _img($v['gluser']['avatar']);
      }
    }
    return $list;
  }

  /**
   * 获取分组里的用户
   */
  public function getUserByGroup($group_id = 0)
  {
    $ids = $this->dao->getIdsByGroupId($group_id);
    $us = new UserService();
    $list = $us->getListAll([['id', 'IN', $ids]],[], ['id,real_name as name,avatar']);
    if ($list) {
      $count = count($list);
      return _layui($count, $list);
    }
    return _layui(0, []);
  }

  public function getUserIdByGroupId($group_id = 0)
  {
    $ids = $this->dao->getIdsByGroupId($group_id);
    return $ids;
  }
}
